package com.kcnet.qk.controller.dynamic;

import android.text.TextUtils;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;

import com.kcnet.qk.constant.Const;
import com.kcnet.qk.constant.StringConst;
import com.kcnet.qk.controller.BasePeopleController;
import com.kcnet.qk.util.FBStringUtil;
import com.kcnet.qk.util.NodeUtil;
import com.kcnet.qk.util.PreferenceHelper;
import com.kcnet.qk.util.RootShell;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jesse.nativelogger.NLogger;

import static com.kcnet.qk.constant.Const.LIST_VIEW;

/**
 * @Auther: 赵浩
 * @Date: 2018-04-27 15:34
 * @Description:
 */
public class TemeLineSendHomeCTR extends BasePeopleController {
    private static TemeLineSendHomeCTR releaseimgctr;
    private static String TAG = "发送主页";

    private int runType;
    private String content;


    private boolean isClickSeatch;
    private boolean isSearchHomeList; //是否进入了搜索主页列表
    private boolean isJoinHomeList; //是否进入了主页列表
    private boolean isJoinHomeDetail = false; //是否进入 主页详情
    private boolean isCLickPosts;
    private boolean isClickShare;
    private boolean isClickSendPosts;

    private boolean isCliCkSelectType;

    private boolean isClickSlectGroup;
    private boolean isCLickSelectFreind;
    private boolean isClickSlectGroupBack;
    private boolean isJoinNext;

    public static TemeLineSendHomeCTR getInstance() {
        if (releaseimgctr == null) {
            synchronized (TemeLineSendHomeCTR.class) {
                if (releaseimgctr == null) {
                    releaseimgctr = new TemeLineSendHomeCTR();
                }
            }
        }
        return releaseimgctr;
    }

    @Override
    public void doTask(AccessibilityEvent event) {
        mClassName = (String) event.getClassName();
        mType = event.getEventType();


        if (!initFbMain && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBLauncherUI)) {
            NLogger.d(TAG, "1、点击搜索框");
            RootShell.click(81 ,38);
            isClickSeatch = true;
            initFbMain = true;
        } else if (isClickSeatch && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "2、事件 点击搜索框-输入主页名称");
            sleepTimedDefault();
            //  Onclic_shell("81", "38");
            inputSayHiSentence(content);

            NLogger.d(TAG, "3、输入完成事件");
            RootShell.keyevent(66);
            isSearchHomeList = true;
            isClickSeatch = false;

        } else if (isSearchHomeList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "4、搜索结果页");

            if (  RootShell.click(378 ,110)) {
                isSearchHomeList = false;
                isJoinHomeList = true;//取消
            }
        } else if (isJoinHomeList && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.support.v7.widget.RecyclerView")
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals("android.support.v7.widget.RecyclerView"))) {
            NLogger.d(TAG, "5、事件 到达搜索主页");
            begin();

        } else if (isJoinHomeDetail && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.katana.activity.ImmersiveActivity")) {
            NLogger.d(TAG, "6、进入了主页详情");
            sleepTimedDefault();
            if(tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_TIEZI))){
                isJoinHomeDetail = false;
                isCLickPosts = true;
            }
        } else if (isCLickPosts) {
            NLogger.d(TAG, "6、进入了主页详情 ");
          //  beginClickShareButton();
            if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_FXAN))){
                isCLickPosts = false;
                if (tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FXAN))) {
                    NLogger.d(TAG, "6、分享按钮点击");
                    isClickShare = true;
                }
            }
         else {
                NLogger.d(TAG, "7、未找到 滑动");
                RootShell.swipe(0,800,0,600);
                //execShellCmd("input swipe 500 800 500 300");
                sleepTime(2000);//适当延迟，给系统反映时间
            }
        } else if (isClickShare && mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals("android.widget.FrameLayout")) {
            NLogger.d(TAG, "8、点击 发帖按钮");
            if(tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FATIE))){
                isClickSendPosts = true;
                isClickShare = false;
            }
        }
        else if (isClickShare && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.4N6")) {
            //進入一個新的判断
            int count = 0;
            NLogger.d(TAG, "8、分享到对应功能模块 count:"+count);
            try {
                 count = getRoot().getChild(0).getChildCount();
            }catch (Exception e){

            }
            if (runType == 1) {
                try {
                    //发帖
                    AccessibilityNodeInfo nodeInfo = getRoot().getChild(0).getChild(0);
                    NLogger.d(TAG, "9、点击发帖 ");
                    if(tagTextClickShell(nodeInfo)){
                        isClickSendPosts = true;
                        isClickShare = false;
                    }
                }catch (Exception e){
                  //  e.printStackTrace();
                    NLogger.e(TAG, "9、点击发帖 失败 任务中止 "+e.getMessage());
                    stopTask();
                }

            } else if (runType == 2) {

                if(count > 5){
                    AccessibilityNodeInfo nodeInfo = getRoot().getChild(0).getChild(5);
                    if(nodeInfo!=null){
                        NLogger.d(TAG, "9、点击一下公开小组分享 "+tagTextClickShell(nodeInfo.getChild(0)));
                        isClickShare = false;
                    }
                }
                else {
                    NLogger.d(TAG, "9- 没有找到小组分享失败 任务结束");
                    stopTask();
                }

              //  isClickSlectGroup = searchTypeClick(FBStringUtil.getString(StringConst.TEAM_XIAOZU));
            } else {
                isCLickPosts = false;
                isClickShare = false;
                Log.d(TAG, "9、分享到好友的时间线条");
                    RootShell.swipe(0,800,0,150);
                    sleepTime(500);//适当延迟，给系统反映时间

                AccessibilityNodeInfo  nodeInfo = getRoot().getChild(0).getChild(count-1);
                    if(nodeInfo!=null){
                        NLogger.d(TAG, "9、点击分享到好友的时间线条 "+tagTextClickShell(nodeInfo));
                        isCLickSelectFreind = true;

                    }
               // }
            }
        }
        else if (isClickSendPosts && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.composer.activity.ComposerActivity")) {
            NLogger.d(TAG, "9、进入到发布界面");
            //分享到FB
            if (runType == 1) {
                NLogger.d(TAG, "10 直接发布到FB");
                sleepTimedDefault();

                 if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_XIAYIBU))){
                    //如果是下一步
                    tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_XIAYIBU));
                    isJoinNext = true;
                } else if(findTextClick(FBStringUtil.getString(StringConst.TIMELINE_FABU))){
                    tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FABU));
                    stopTask();
                }

            } else {
                //分享到小组
                isClickSendPosts = false;
                isCliCkSelectType = RootShell.click(86,57);//"input tap 86 57"); //点击弹出选择框
            }

        } else if (isCliCkSelectType && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("android.widget.FrameLayout")) {
            NLogger.d(TAG, "10 选择对应模块 小组还是好友时间线条");
            if (runType == 1) {
            } else if (runType == 2) {
                isClickSlectGroup = searchTypeClick(FBStringUtil.getString(StringConst.TEAM_XIAOZU));
            } else {
                isCLickSelectFreind = searchTypeClick(FBStringUtil.getString(StringConst.HDYH_HYDSJX));
            }
        }
        //进入到选择小组
        else if (isClickSlectGroup && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            NLogger.d(TAG, "11 选择小组");
            clickSelectGroupFist();
        }
        //进入到选择好友
        else if (isCLickSelectFreind && (mType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED && mClassName.equals(LIST_VIEW)
                || mType == AccessibilityEvent.TYPE_VIEW_SCROLLED && mClassName.equals(LIST_VIEW))) {
            NLogger.d(TAG, "11 选择好友");
            clickSelectGroupFist();
        } else if (isClickSlectGroupBack && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("com.facebook.composer.activity.ComposerActivity")) {
            NLogger.d(TAG, "12、发布");
            if (searchTypeClick(FBStringUtil.getString(StringConst.TIMELINE_FABU))) {
                stopTask();
            }
        }
        else if (isJoinNext && mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals(Const.FbUI.FBIMMERUI)) {
            NLogger.d(TAG,"11. 最后一步 发布发布到FaceBook");
            if(RootShell.click(487,135)){
                sleepTimedDefault();
                RootShell.click(456,885);
                stopTask();
            }
        }else if(mType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED && mClassName.equals("X.3Hl")){
            tagTextClick(FBStringUtil.getString(StringConst.TIMELINE_FENXIANG));
            stopTask();
        }
    }


    private void begin() {

        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, "android.support.v7.widget.RecyclerView");
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                    //  ToastUtil.showToast(AppContext.getInstance().getContext(),"scrllorNodeInfo listView null");
                    return;
                }

                for (int q = 0; q < scrllorNodeInfo.getChildCount(); q++) {


                    AccessibilityNodeInfo nodeInfo = scrllorNodeInfo.getChild(q);
                    if (nodeInfo.getChildCount() == 0) {
                        continue;
                    }
                    String homeDesc = "";
                    try {
                        homeDesc = nodeInfo.getChild(0).getChild(0).getChild(0).getContentDescription().toString();
                    }catch (Exception e){
                        //e.printStackTrace();
                    }
                    if(TextUtils.isEmpty(homeDesc)){
                        continue;
                    }
                    Log.d("ND", "nodeInfo size" + nodeInfo.getChildCount());
                    //sleepTimedDefault();
                    isJoinHomeDetail = tagTextClickShell(nodeInfo);
                    //  isJoinHomeDetail =  true;
                    NLogger.d(TAG, "点击了进入主页" + isJoinHomeDetail);
                    if (isJoinHomeDetail) {
                        isJoinHomeList = false;
                        currentAddCount++;
                        break;
                    }
                }
            }
        }
    }


    public void clickSelectGroupFist() {
        AccessibilityNodeInfo scrllorNodeInfo = null;
        AccessibilityNodeInfo root = getRoot();
        if (root != null) {
            synchronized (mLock) {
                List<AccessibilityNodeInfo> scrllorNodeInfos = NodeUtil.traverseNodeByClassList(root, LIST_VIEW);
                if (scrllorNodeInfos.size() > 0) {
                    scrllorNodeInfo = scrllorNodeInfos.get(0);
                }
                if (scrllorNodeInfo == null) {
                   // ToastUtil.showToast(AppContext.getInstance().getContext(), "scrllorNodeInfo listView null");
                    return;
                }
                if (scrllorNodeInfo.getChildCount() > 2) {
                    AccessibilityNodeInfo as = scrllorNodeInfo.getChild(2);
                    isClickSlectGroupBack = NodeUtil.performClick(as);
                    if (isClickSlectGroupBack) {
                        isClickSlectGroup = false;
                    }
                    NLogger.d(TAG, "点击了第一个 小组" + isClickSlectGroupBack);
                }else {
                    NLogger.d(TAG, "任务结束因为找不到对应小组或者好友");
                    stopTask();
                }

            }
        }
    }



    public void setParms(int minAddTime, int maxAddTime, String content, int runType) {
       // stopTask();
        EventBus.getDefault().post(new Boolean(true));
        PreferenceHelper.putBoolean(Const.PREF_KEY_STOP_AUTO_FLAG, true);
        this.minAddTime = minAddTime;
        this.maxAddTime = maxAddTime;
        this.currentAddCount = 0;
        this.runType = runType;
        this.content = content;
        initFbMain = false;

        isClickSeatch = false;
        isSearchHomeList = false;
        isJoinHomeList = false;
        isJoinHomeDetail = false;
        isCLickPosts = false;
        isClickShare = false;
        isClickSendPosts = false;

        isCliCkSelectType = false;

        isClickSlectGroup = false;
        isCLickSelectFreind = false;
        isClickSlectGroupBack = false;
        isJoinNext =false;

    }


}
